package com.machine.action.search;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.machine.bean.Componentpart;
import com.machine.bean.PageBean;
import com.machine.dao.user.ComponentDao;
import com.machine.service.user.ComponentServiceDao;
import com.machine.util.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author Administrator
 * 
 */
@Component("ComponentSearch")
@Scope(value = "prototype")
public class ComponentSearchAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private ComponentDao componentDao;
	private ComponentServiceDao componentServiceDao;
	private String tag;
	
	private String orderKey;
	private String orderValue;
	
	
	
	
	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public String getOrderKey() {
		return orderKey;
	}

	public void setOrderKey(String orderKey) {
		this.orderKey = orderKey;
	}

	public String getOrderValue() {
		return orderValue;
	}

	public void setOrderValue(String orderValue) {
		this.orderValue = orderValue;
	}

	public ComponentServiceDao getComponentServiceDao() {
		return componentServiceDao;
	}

	@Resource
	public void setComponentServiceDao(ComponentServiceDao componentServiceDao) {
		this.componentServiceDao = componentServiceDao;
	}

	private int page; // 第几页

	private PageBean pageBean; // 包含分布信息的bean

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public ComponentDao getComponentDao() {
		return componentDao;
	}
	
	@Resource
	public void setComponentDao(ComponentDao componentDao) {
		this.componentDao = componentDao;
	}

	@Override
	public String execute() throws Exception {
		if (page <= 0) {
			page = 1;
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
	    request.setCharacterEncoding("UTF-8");  
	    response.setContentType("text/html;charset=UTF-8");  
		String keyword = request.getParameter("keywords");
	//	keyword=URLDecoder.decode(keyword, "UTF-8");  
		request.getSession().setAttribute("keywords", keyword);
//		System.out.println(keyword);
		if(tag != null){
			request.getSession().setAttribute("tag",tag );
		}else{
			
			request.getSession().setAttribute("tag","" );
		}
		
		if(orderKey != null && orderKey != ""){
			request.getSession().setAttribute("orderKey", orderKey);
		}
		if(orderValue!= null &&  orderValue !=""){
			request.getSession().setAttribute("orderValue", orderValue);
		}
		
		
		System.out.println("tag : " + tag);
		
		componentDao.insertNewKeywords(keyword);
		
		//List<Componentpart> comps = componentDao.find(keyword,page,5);
		
		//pageBean = componentServiceDao.querySearchForPage(keyword,2, page);
		pageBean = componentServiceDao.querySearchForPage(keyword,2, page,  (String)request.getSession().getAttribute("tag"),  (String)request.getSession().getAttribute("orderKey"),  (String)request.getSession().getAttribute("orderValue"));
		List<Componentpart> comps = pageBean.getList();
		JSONObject json = new JSONObject();
		
		int count = 0;
		List<String> names = new ArrayList<String>();
		List<String> specifications = new ArrayList<String>();
		List<String> materialqualitys = new ArrayList<String>();
		List<String> providerids = new ArrayList<String>();
		List<String> outtimes = new ArrayList<String>();
		List<String> comnums = new ArrayList<String>();
		List<Float> prices = new ArrayList<Float>();
		List<Integer> ids = new ArrayList<Integer>();
		List<String> sellNum = new ArrayList<String>();
		List<String> stockNum = new ArrayList<String>();
		
		
		List<String> counts = new ArrayList<String>();
		for(Componentpart c : comps){	
			names.add(c.getName());
			specifications.add(c.getSpecification());
			materialqualitys.add(c.getMaterialquality());
			providerids.add(String.valueOf(c.getProviderid()));
			outtimes.add(c.getOuttime());
			comnums.add(c.getComponentnumber());
			count = comps.size();
			counts.add("1");
			prices.add(c.getPrice());
			ids.add(c.getId());
			sellNum.add(String.valueOf(c.getSellnum()));
			stockNum.add(String.valueOf(c.getStockamount()));
		}
		
		json.put("name", names);
		//json.put("specification", specifications);
		//json.put("materialquality", materialqualitys);
		json.put("providerid", providerids);
		json.put("outtime", outtimes);
		//json.put("componentnum", comnums);
		json.put("size", count);
		json.put("price", prices);
		json.put("id", ids);
		json.put("sell", sellNum);
		json.put("stock", stockNum);
		//System.out.println(c.getName());
//		System.out.println(json.toString());
		
		request.setAttribute("json", json.toString());
		request.setAttribute("count", counts);
		return SUCCESS;
	}

}
